package com.friday.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.bean.Inquiry;
import com.friday.dao.InquiryDao;
import com.friday.util.Trans;
import com.friday.util.UploadImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class InquiryController {

    @Autowired
    private InquiryDao inquiryDao;

    @RequestMapping("/inquiryList")
    public JSONArray getAllShowInquiry(){
        return inquiryDao.getAllShowInquiry();
    }

    @RequestMapping("/inquiryAgentList")
    public JSONArray getAgentInquiry(@RequestParam("id") int id){
        return inquiryDao.getAgentInquiry(id);
    }


    @RequestMapping("/inquiryDetail")
    public JSONObject getInquiryDtlById(@RequestParam("id") int id){
        JSONObject inquiryAndDetail =inquiryDao.getShowInquiryById(id);
        JSONArray detail = inquiryDao.getShowInquiryDtlById(id);
        for(int i=0;i<detail.size();i++){
            detail.getJSONObject(i).put("key",i);
        }
        inquiryAndDetail.put("goods",detail);
        inquiryAndDetail.put("totalExpectprice",inquiryDao.getTotalExp(id));
        return inquiryAndDetail;
    }

    //返回询价单的产品信息
    @RequestMapping("/inquiryProductList")
    public JSONArray getInquiryProduct(@RequestParam("id") int id){
        return inquiryDao.getShowInquiryDtlById(id);
    }

    @RequestMapping("/inquiryEdit")
    public String newInquiry(@RequestBody JSONObject jsonObject){
        int id=jsonObject.getInteger("inquiryid");
        JSONArray data=jsonObject.getJSONArray("dataSource");
        int tag=0;
        for(int i=0;i<data.size();i++){
            JSONObject dtl = data.getJSONObject(i);
            tag+=inquiryDao.setTransRate(id,dtl.getInteger("goods_id"),dtl.getInteger("trans_rate"));
        }
        if(tag==data.size()){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/inquiryMatch")
    public JSONArray inquiryMatch(@RequestParam("id") int id,@RequestParam("authority") String authority){
        return inquiryDao.getAllmatchInquiry(id,authority);
    }

    @RequestMapping("/inquiryAdd")
    public JSONObject addInquiry(@RequestBody JSONObject inquiry){
        JSONObject result=new JSONObject();
        result.put("result","fail");
        int tag1=inquiryDao.addInquiry(inquiry);
        if(tag1>0){
            int tag2=0;
            int inquiry_id = inquiry.getInteger("id");
            JSONArray inquirydtl=inquiry.getJSONArray("goods");
            for(int i=0;i<inquirydtl.size();i++){
                inquirydtl.getJSONObject(i).put("inquiry_id",inquiry_id);
                tag2+=inquiryDao.addInquirydtl(inquirydtl.getJSONObject(i));
            }
            if(tag2==inquirydtl.size()){
                result.put("result","success");
                result.put("inquiry_id",inquiry_id);
            }
        }
        return result;
    }


    @RequestMapping("/inquiryDelete")
    public String delInquiry(@RequestParam int id){
        int delFromInquiry=inquiryDao.delInquiry(id);
        int delFromInquirydtl=inquiryDao.delInquirydtl(id);
        if(delFromInquiry>0 && delFromInquirydtl>0){
            return "success";
        }
        return "fail";
    }





}
